/**
 * 版权所有 2009-2012山东新北洋信息技术股份有限公司
 * 保留所有权利。
 */
package com.linyaonan.leetcode.medium._32_01;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @ProjectName: leetcode
 * @Package: com.linyaonan.leetcode.medium._32_01
 * @ClassName: LevelOrder
 * @Author: linyaonan
 * @Date: 2020/3/30 16:14
 */
public class LevelOrder {

    public int[] levelOrder(TreeNode root) {

        List<Integer> result = new ArrayList<>();
        if (root == null) {
            return new int[0];
        }
        LinkedList<TreeNode> nodes = new LinkedList<>();
        nodes.push(root);
        while (!nodes.isEmpty()) {
            TreeNode pop = nodes.poll();
            result.add(pop.val);
            TreeNode left = pop.left;
            TreeNode right = pop.right;
            if (left != null) {
                nodes.add(left);
            }
            if (right != null) {
                nodes.add(right);
            }
        }
        int[] ints = new int[result.size()];
        for (int i = 0; i < result.size(); i++) {
            ints[i]= result.get(i);
        }

        return ints;
    }

}
